package hibu.LeiJonGun.activities;
import hibu.LeiJonGun.seniorservice.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

//Created by Elis Tor Jonsson. 
//An activity class where all assignment categories are shown

public class ActAssignmentCategory extends Activity{
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.act_category, menu);
		return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle menu click
        switch (item.getItemId()) {
        case R.id.act_category_help:
        	String url = "http://www.lillesto.net/hjelpeside/index.html";
        	Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        	startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_assignment_category);

		final ImageButton imgBtnHousework= (ImageButton) findViewById(R.id.btnHousework);
		final TextView lblHousework = (TextView) findViewById(R.id.lblHousework);
		final LinearLayout layoutHousework = (LinearLayout) findViewById(R.id.layoutHousework);
		
		final ImageButton imgBtnOutdoorWork = (ImageButton) findViewById(R.id.btnOutdoorWork);
		final TextView lblOutdoorWork = (TextView) findViewById(R.id.lblOutdoorWork);
		final LinearLayout layoutOutdoorWork = (LinearLayout) findViewById(R.id.layoutOutdoorWork);
		
		final ImageButton imgBtnTransport= (ImageButton) findViewById(R.id.btnTransport);
		final TextView lblTransport = (TextView) findViewById(R.id.lblTransport);
		final LinearLayout layoutTransport = (LinearLayout) findViewById(R.id.layoutTransport);
		
		final ImageButton imgBtnUserdefined= (ImageButton) findViewById(R.id.btnUserdefined);
		final TextView lblUserdefined = (TextView) findViewById(R.id.lblUserdefined);
		final LinearLayout layoutUserdefined = (LinearLayout) findViewById(R.id.layoutUserdefined);
		
		final ImageButton imgBtnCooking= (ImageButton) findViewById(R.id.btnCooking);
		final TextView lblCooking = (TextView) findViewById(R.id.lblCooking);
		final LinearLayout layoutCooking = (LinearLayout) findViewById(R.id.layoutCooking);
		
		final ImageButton imgBtnShopping= (ImageButton) findViewById(R.id.btnShopping);
		final TextView lblShopping= (TextView) findViewById(R.id.lblShopping);
		final LinearLayout layoutShopping= (LinearLayout) findViewById(R.id.layoutShopping);
		
		final View.OnTouchListener onPressedCategory = new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				String selected = v.getTag().toString();
				// When user push down
				// Go to pressed image/textcolor version
				if (event.getAction()==MotionEvent.ACTION_DOWN) {; 
					// Check witch layout,image or text that was pressed
					if (selected.equals("Husarbeid")) { //Housework
						lblHousework.setTextColor(getResources().getColor(R.color.lblTextPressed));
						imgBtnHousework.setBackgroundResource(R.drawable.housepressed);
					}else if(selected.equals("Utearbeid")) { //Outdoor work
						lblOutdoorWork.setTextColor(getResources().getColor(R.color.lblTextPressed));
						imgBtnOutdoorWork.setBackgroundResource(R.drawable.outdoorpressed);
					}else if(selected.equals("Transport")) {
						lblTransport.setTextColor(getResources().getColor(R.color.lblTextPressed));
						imgBtnTransport.setBackgroundResource(R.drawable.transportpressed);
					}else if(selected.equals("Egendefinert")) { //customize
						lblUserdefined.setTextColor(getResources().getColor(R.color.lblTextPressed));
						imgBtnUserdefined.setBackgroundResource(R.drawable.userdefinedpressed);
					}else if(selected.equals("Matlaging")) { //cooking
						lblCooking.setTextColor(getResources().getColor(R.color.lblTextPressed));
						imgBtnCooking.setBackgroundResource(R.drawable.cookingpressed);
					}else if(selected.equals("Handling")) { //shopping
						lblShopping.setTextColor(getResources().getColor(R.color.lblTextPressed));
						imgBtnShopping.setBackgroundResource(R.drawable.shoppingpressed);
					}
					
				// When user let go (release view)
				// Go to default image/textcolor 
				} else if(event.getAction()==MotionEvent.ACTION_UP){
					// Check which layout,image or text that was pressed
					if (selected.equals("Husarbeid")) { //housework
						lblHousework.setTextColor(getResources().getColor(R.color.BtnCategoryText));
						imgBtnHousework.setBackgroundResource(R.drawable.house);
						nextActivity(selected);
					}else if(selected.equals("Utearbeid")) { //outdoor work
						lblOutdoorWork.setTextColor(getResources().getColor(R.color.BtnCategoryText));
						imgBtnOutdoorWork.setBackgroundResource(R.drawable.outdoor);
						nextActivity(selected);
					}else if(selected.equals("Transport")) {
						lblTransport.setTextColor(getResources().getColor(R.color.BtnCategoryText));
						imgBtnTransport.setBackgroundResource(R.drawable.transport);
						startSummaryActivity(selected,false);
					}else if(selected.equals("Egendefinert")) { //customize
						lblUserdefined.setTextColor(getResources().getColor(R.color.BtnCategoryText));
						imgBtnUserdefined.setBackgroundResource(R.drawable.userdefined);
						startSummaryActivity(selected,false);
					}else if(selected.equals("Matlaging")) { //cooking
						lblCooking.setTextColor(getResources().getColor(R.color.BtnCategoryText));
						imgBtnCooking.setBackgroundResource(R.drawable.cooking);
						startSummaryActivity(selected,false);
					}else if(selected.equals("Handling")) { //shopping
						lblShopping.setTextColor(getResources().getColor(R.color.BtnCategoryText));
						imgBtnShopping.setBackgroundResource(R.drawable.shopping);
						nextActivity(selected);
					}
					
				}
				return true;
			}
				
		};
		
		//Add ontouch listener to layout 
		layoutHousework.setOnTouchListener(onPressedCategory);
		imgBtnHousework.setOnTouchListener(onPressedCategory);
		lblHousework.setOnTouchListener(onPressedCategory);
		
		imgBtnOutdoorWork.setOnTouchListener(onPressedCategory);
		lblOutdoorWork.setOnTouchListener(onPressedCategory);
		layoutOutdoorWork.setOnTouchListener(onPressedCategory);
		
		imgBtnTransport.setOnTouchListener(onPressedCategory);
		lblTransport.setOnTouchListener(onPressedCategory);
		layoutTransport.setOnTouchListener(onPressedCategory);
		
		imgBtnUserdefined.setOnTouchListener(onPressedCategory);
		lblUserdefined.setOnTouchListener(onPressedCategory);
		layoutUserdefined.setOnTouchListener(onPressedCategory);
		
		imgBtnCooking.setOnTouchListener(onPressedCategory);
		lblCooking.setOnTouchListener(onPressedCategory);
		layoutCooking.setOnTouchListener(onPressedCategory);
		
		imgBtnShopping.setOnTouchListener(onPressedCategory);
		lblShopping.setOnTouchListener(onPressedCategory);
		layoutShopping.setOnTouchListener(onPressedCategory);
		

	}
	
	private void startSummaryActivity(String category, Boolean add){
		Intent intentSummary = new Intent(ActAssignmentCategory.this, ActAssignmentSummary.class);
		intentSummary.putExtra("category",category);
		intentSummary.putExtra("assignmentText",category);
		intentSummary.putExtra("add",add);
		startActivity(intentSummary);
		finish();
	}
	
	private void nextActivity(String category){
		Intent intentSummary = null;
		if (category.equals("Husarbeid")) intentSummary = new Intent(ActAssignmentCategory.this, ActAssignmentHousework.class); //Housework
		if (category.equals("Utearbeid")) intentSummary = new Intent(ActAssignmentCategory.this, ActAssignmentOutdoorwork.class); //outdoor work
		if (category.equals("Handling")) intentSummary = new Intent(ActAssignmentCategory.this, ActAssignmentShopping.class); //shopping
		intentSummary.putExtra("category",category); 
		startActivity(intentSummary);
		finish();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	        this.finish();
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
}
